package com.education.platform.common.web;


import com.education.platform.common.exception.BusinessException;
import com.education.platform.common.exception.ParameterException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * 全局异常处理
 */
@ControllerAdvice
public class GlobalExceptionHandler {

	private static final Logger LOGGER = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    /**
     * 异常捕获
     * @param e
     * @return
     */
	@ExceptionHandler(Exception.class)
	@ResponseBody
	public ResponseEntity exceptionHandler(Exception e) {
		ResponseEntity responseEntity = new ResponseBuilder()
                .code(ResponseCode.INTERNAL_ERROR.getCode())
                .message(ResponseCode.INTERNAL_ERROR.getDesc())
                .build();
		LOGGER.error("系统繁忙!!", e);
		return responseEntity;
	}

    /**
     * 异常捕获
     * @param e
     * @return
     */
    @ExceptionHandler(MissingServletRequestParameterException.class)
    @ResponseBody
    public ResponseEntity missingServletRequestParameterHandler(HttpRequestMethodNotSupportedException e) {
        ResponseEntity responseEntity = new ResponseBuilder()
                .code(ResponseCode.INTERNAL_ERROR.getCode())
                .message(e.getMessage())
                .build();
        LOGGER.error("参数缺失!!", e.getMessage());
        return responseEntity;
    }
    /**
     * 异常捕获
     * @param e
     * @return
     */
    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    @ResponseBody
    public ResponseEntity methodNotSupportedHandler(HttpRequestMethodNotSupportedException e) {
        ResponseEntity responseEntity = new ResponseBuilder()
                .code(ResponseCode.INTERNAL_ERROR.getCode())
                .message(e.getMessage())
                .build();
        LOGGER.error("请求方式不被支持!!", e.getMessage());
        return responseEntity;
    }
    /**
     * 业务异常捕获
     * @param e
     * @return
     */
	@ExceptionHandler(BusinessException.class)
	@ResponseBody
	public ResponseEntity businessException(BusinessException e) {
        ResponseEntity responseEntity = new ResponseBuilder()
                .code(e.getCode())
                .message(e.getMessage())
                .build();
		LOGGER.error("业务异常!!", e);
		return responseEntity;
	}

    /**
     * 参数异常捕获
     * @param e
     * @return
     */
    @ExceptionHandler(ParameterException.class)
    @ResponseBody
    public ResponseEntity parameterException(ParameterException e) {
        ResponseEntity responseEntity = new ResponseBuilder()
                .code(ResponseCode.VALIDATE_FAIL.getCode())
                .message(e.getMessage())
                .build();
        LOGGER.error("参数异常!!", e);
        return responseEntity;
    }


    /**
     * valid 验证异常处理
     * @param e
     * @return
     */
	@ExceptionHandler(MethodArgumentNotValidException.class)
	@ResponseBody
	public ResponseEntity MethodArgumentNotValidException(MethodArgumentNotValidException e) {
        ResponseEntity responseEntity = new ResponseBuilder()
                .code(ResponseCode.VALIDATE_FAIL.getCode())
                .build();
		BindingResult bindingResult = e.getBindingResult();
		if (bindingResult.hasErrors()) {
			List<FieldError> errors = bindingResult.getFieldErrors();
			if (!CollectionUtils.isEmpty(errors)) {
				responseEntity.setMessage(errors.get(0).getDefaultMessage());
			}
		}
        LOGGER.error("验证异常!!", e);
		return responseEntity;
	}

}
